<%--
    Document   : indexs
    Created on : 16 févr. 2011, 14:24:19
    Author     : Nishruu
--%>

<%@page import="java.util.HashMap"%>
<%@page import="modele.Equipe"%>
<%@page import="modele.Projet"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags/" prefix="inc" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<inc:header/>
<script src="javascripts/prototype.js" type="text/javascript"></script>
<script src="javascripts/scriptaculous.js" type="text/javascript"></script>

<div id="contenu">
    <div id="gauche">
        <div id="valeurs">
            <div style="position:relative" class="gantt" id="GanttChartDIV"></div>
            <%
             RequestDispatcher rd = request.getRequestDispatcher("/MainServlet?type=getProjetsByUser");
             rd.include(request, response);
             ArrayList<Projet> projets = (ArrayList) request.getAttribute("liste_projets");
             HashMap<Integer, ArrayList<Equipe>> equipesByProjet = (HashMap<Integer, ArrayList<Equipe>>) request.getAttribute("liste_equipesByProjet");
            %>

            
            <script language="javascript">

            var lock = false;
            var g = new JSGantt.GanttChart('g',document.getElementById('GanttChartDIV'), 'month');
            g.setShowRes(0); // Show/Hide Responsible (0/1)
            g.setShowDur(0); // Show/Hide Duration (0/1)
            g.setShowComp(0); // Show/Hide % Complete(0/1)
            g.setCaptionType('none');  // Set to Show Caption (None,Caption,Resource,Duration,Complete)
            g.setShowStartDate(0); // Show/Hide Start Date(0/1)
            g.setShowEndDate(0); // Show/Hide End Date(0/1)
            g.setDateDisplayFormat("dd/mm/yy");
            g.setDateInputFormat("yyyy-mm-dd");

        //TaskItem(pID, pName, pStart, pEnd, pColor, pLink, pMile, pRes, pComp, pGroup, pParent, pOpen, pDepend)
        //pID: (required) is a unique ID used to identify each row for parent functions and for setting dom id for hiding/showing
        //pName: (required) is the task Label
        //pStart: (required) the task start date, can enter empty date ('') for groups. You can also enter specific time (2/10/2008 12:00) for additional percision or half days.
        //pEnd: (required) the task end date, can enter empty date ('') for groups
        //pColor: (required) the html color for this task; e.g. '00ff00'
        //
        //pLink: (optional) any http link navigated to when task bar is clicked.
        //pMile:(optional) represent a milestone
        //pRes: (optional) resource name
        //
        //pComp: (required) completion percent
        //
        //pGroup: (optional) indicates whether this is a group(parent) - 0=NOT Parent; 1=IS Parent
        //
        //pParent: (required) identifies a parent pID, this causes this task to be a child of identified task
        //
        //pOpen: can be initially set to close folder when chart is first drawn
        //
        //pDepend: optional list of id's this task is dependent on ... line drawn from dependent to this item
        //pCaption: optional caption that will be added after task bar if CaptionType set to "Caption"

        

    var racine = document.createElement("div");
    racine.id = "racine";

        <% if(projets!=null){

            int id_session = -1;
            String nom_session = "";

            for (int i=0; i<projets.size(); i++){

 
                int test_id_session = projets.get(i).getSession_diplome().getId_session();
                if (test_id_session != id_session) {
                    id_session = test_id_session;
                    nom_session = projets.get(i).getSession_diplome().getNom();
                    %>
                    g.AddTaskItem(new JSGantt.TaskItem(<%=id_session%>,'<%=nom_session%>','','','ff0000','',0,'',0,1,0,0,0,0));


                    neoelement1 = document.createElement("div");
                    neoelement1.id = "<%=id_session%>";
                    neoelement1.style.display = 'none';
                    neoelement1.innerHTML =
                        "<%=nom_session%><br/>" + 
                        "<input type='button' onClick=popup('createProjet',<%= id_session %>) value='Ajouter un projet' />";
                    racine.appendChild(neoelement1);
                    <%

                }

                int id_projet = projets.get(i).getId_projet();
                int taille = equipesByProjet.get(id_projet).size();
                String nom2 = projets.get(i).getNom();
                %>

                g.AddTaskItem(new JSGantt.TaskItem(<%=i+1%>,'<%=nom2%>','<%=projets.get(i).getDate_debut()%>','<%=projets.get(i).getDate_fin() %>','ff0000','',0,'',0,<%= (taille==0) ? 0 : 1 %>,<%=id_session %>,0,0,0));

                    id_proj =
                    neoelement2 = document.createElement("div");
                    neoelement2.id = "<%=i+1%>";
                    neoelement2.style.display = 'none';
                    neoelement2.innerHTML = "<%=nom2%><br/>" +
                        "<input type='button' onClick=popup('updateProjet',<%= id_projet %>) value='Modifier le projet' />" +
                        "<input type='button' onClick=popup('suppressProjet',<%= id_projet %>) value='Supprimer le projet' />" +
                        "<input type='button' onClick=popup('createEquipe',<%= id_projet %>) value='Ajouter une équipe' />";
                    racine.appendChild(neoelement2);
                <%
                if(equipesByProjet.get(id_projet)!=null){
                    for (int j=0; j<equipesByProjet.get(id_projet).size(); j++){
                        String nom3 = equipesByProjet.get(id_projet).get(j).getNom();
                        int id_equipe = equipesByProjet.get(id_projet).get(j).getId_equipe();
                        %>

                        
                        g.AddTaskItem(new JSGantt.TaskItem(<%=i+1%><%=j+1%>,'<%=nom3%>','<%=equipesByProjet.get(id_projet).get(j).getDate_debut()%>','<%=equipesByProjet.get(id_projet).get(j).getDate_fin()%>', 'ff00ff', '', 0,'',0,0,<%=i+1%>,0,0,0));
                        neoelement3 = document.createElement("div");
                        neoelement3.id = "<%=i+1%><%=j+1%>";
                        neoelement3.style.display = 'none';
                        neoelement3.innerHTML = "<%=nom3%><br/>" +
                            "<input type='button' onClick=('updateEquipe',<%= id_equipe %>) value='Modifier l&#39;équipe' />" +
                            "<input type='button' onClick=('suppressEquipe',<%= id_equipe %>) value='Supprimer l&#39;équipe' />" +
                            "<input type='button' onClick=('addMembretoEquipe',<%= id_equipe %>) value='Ajouter un membre à l&#39;équipe' />";
                        racine.appendChild(neoelement3);
                <%
                    }
                }
            }
        }

%>
            if( g ) {

                g.Draw();
                g.DrawDependencies();
            }
            else {
                alert("not defined");

            }

            var last_visible = null;
            var choisi;
            var memoireCouleur;

            function popup(type,id) {
                switch(type) {
                    case 'updateProjet': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                    case 'suppressProjet': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                    case 'createEquipe': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                    case 'updateEquipe': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                    case 'suppressEquipe': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                    case 'createProjet': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                    case 'addMembretoEquipe': window.open('popupaction.jsp?type=' + type + '&id=' + id); break;
                }
            }

            function switchLock(id_projet) {
                if (lock) {
                    document.getElementById('child_'+choisi).style.backgroundColor = memoireCouleur;
                    if(id_projet.valueOf()!=choisi) {
                           memoireCouleur = document.getElementById('child_'+id_projet.valueOf()).style.backgroundColor;
                           document.getElementById('child_'+id_projet.valueOf()).style.backgroundColor = '#CCC';

                           choisi = id_projet;
                    }
                    else {
                        lock = false;
                        choisi = null;
                    }
                                           


                }
                else {
                    lock = true;

                    memoireCouleur = document.getElementById('child_'+id_projet.valueOf()).style.backgroundColor;
                    document.getElementById('child_'+id_projet.valueOf()).style.backgroundColor = '#CCC';
                    choisi = id_projet;
                }

                chargerCadre(id_projet,true);
            }

            function chargerCadre(id_projet,fromlock) {
                if(!lock || fromlock) {
                    $('ohoh').fade(
                        {
                            duration: 0.4,
                            afterFinish: function() {
                                if(last_visible != null) last_visible.style.display = 'none';
                                last_visible = document.getElementById(id_projet.valueOf());
                                last_visible.style.display = 'block';

                                $('ohoh').appear(
                                    {
                                    duration: 0.4
                                    }
                                )
                            }
                        }
                    );
                }
            }

            </script>
        
        </div>
    </div>
    <div id="droite">
        <div id="news">
            <form name='mainForm' method="POST">
            <div id='ohoh'>
                <h3>Cliquez sur un projet dans le diagramme de gauche</h3>
            </div>
            </form>
        </div>
    </div>
</div>


<inc:footer/>

<script language="javascript">
    $('ohoh').appendChild(racine);
</script>
